package guilayer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import modellayer.Company;
import modellayer.KeyPerson;
import modellayer.ZipCode;
import modellayer.Note;
import controllerlayer.AppointmentCtrl;
import controllerlayer.CompanyCtrl;
import controllerlayer.KeyPersonCtrl;
import controllerlayer.TimestampCtrl;
import controllerlayer.ZipCodeCtrl;
import controllerlayer.NoteCtrl;

import java.awt.Color;

public class CreateNote extends JFrame
{	
	private JPanel contentPane;
	private JTextField txtTitle;	
//	private JTextField txtText;	
	private TimestampCtrl tc = new TimestampCtrl();	
//	private ZipCodeCtrl zcc = new ZipCodeCtrl();
//	private ArrayList<Integer> quarters = new ArrayList<Integer>();
//	private ArrayList<Integer> hours = new ArrayList<Integer>();
//	private ArrayList<Integer> days = new ArrayList<Integer>();
//	private ArrayList<Integer> months = new ArrayList<Integer>();
//	private ArrayList<Integer> years = new ArrayList<Integer>();
	private JTextArea txtText;
//	private JComboBox cbFromDay;
//	private JComboBox cbFromMonth;
//	private JComboBox cbFromYear;
	private JComboBox cbKeyPerson;	
//	private JComboBox cbFromHour;
//	private JComboBox cbFromQuarter;
//	private JComboBox cbToDay;
//	private JComboBox cbToMonth;
//	private JComboBox cbToYear;
//	private JComboBox cbToHour;
//	private JComboBox cbToQuarter;
//	private int yearCountMin = tc.getYear() - 10;
//	private int yearCountMax = tc.getYear() + 10;
//	private int monthCount = 1;
//	private int dayCount = 1;
//	private int hourCount = 0;
//	private int quarterCount = 00;
	private JComboBox cbCompany;
//	private int zipLength = 0;
	private JLabel lblLblerror;
	
	/**
	 * Create the frame.
	 */	
	public CreateNote() 
	{
		/**
		 * Setup frame
		 */
		setTitle("Fairway Networks | Opret Note");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 475, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		getContentPane().setLayout(null);
		contentPane.setLayout(null);
		
		
		/**
		 * Set up JLabels
		 */		
		JLabel lblKeyPerson = new JLabel("N\u00F8gleperson:");
		lblKeyPerson.setBounds(10, 11, 124, 14);
		getContentPane().add(lblKeyPerson);
		
		JLabel lblText = new JLabel("Tekst:");
		lblText.setBounds(10, 142, 124, 14);
		getContentPane().add(lblText);
		
//		JLabel lblFromDate = new JLabel("Fra:");
//		lblFromDate.setBounds(10, 42, 124, 14);
//		getContentPane().add(lblFromDate);
//		
//		JLabel lblToDate = new JLabel("Til:");
//		lblToDate.setBounds(10, 73, 124, 14);
//		contentPane.add(lblToDate);
		
		JLabel lblCompany = new JLabel("Virksomhed:");
		lblCompany.setBounds(10, 54, 124, 14);
		getContentPane().add(lblCompany);
		
		JLabel lblTitle = new JLabel("Titel:");
		lblTitle.setBounds(10, 95, 124, 14);
		getContentPane().add(lblTitle);
		
		/**
		 * Setup input areas
		 */		
		cbKeyPerson = new JComboBox(listOfKPNames());
		cbKeyPerson.setEditable(false);
		cbKeyPerson.setBounds(144, 8, 305, 20);
		contentPane.add(cbKeyPerson);
		
//		cbFromDay = new JComboBox(listOfDays());
//		cbFromDay.setToolTipText("Dag");
//		cbFromDay.setEditable(false);
//		cbFromDay.setSelectedItem(tc.getDay());
//		cbFromDay.setBounds(144, 39, 53, 20);
//		contentPane.add(cbFromDay);
//		
//		cbFromMonth = new JComboBox(listOfMonths());
//		cbFromMonth.setToolTipText("M\u00E5ned");
//		cbFromMonth.setEditable(false);
//		cbFromMonth.setSelectedItem(tc.getMonth());
//		cbFromMonth.setBounds(207, 39, 53, 20);
//		contentPane.add(cbFromMonth);
//		
//		cbFromYear = new JComboBox(listOfYears());
//		cbFromYear.setToolTipText("\u00C5r");
//		cbFromYear.setEditable(false);
//		cbFromYear.setSelectedItem(tc.getYear());
//		cbFromYear.setBounds(270, 39, 53, 20);
//		contentPane.add(cbFromYear);
//		
//		cbFromHour = new JComboBox(listOfHours());
//		cbFromHour.setBounds(333, 39, 53, 20);
//		contentPane.add(cbFromHour);
//		
//		cbFromQuarter = new JComboBox(listOfQuarters());
//		cbFromQuarter.setBounds(396, 39, 53, 20);
//		contentPane.add(cbFromQuarter);
//		
//		cbToDay = new JComboBox(listOfDays());
//		cbToDay.setToolTipText("Dag");
//		cbToDay.setEditable(false);
//		cbToDay.setSelectedItem(tc.getDay());
//		cbToDay.setBounds(144, 70, 53, 20);
//		contentPane.add(cbToDay);
//		
//		cbToMonth = new JComboBox(listOfMonths());
//		cbToMonth.setToolTipText("M\u00E5ned");
//		cbToMonth.setEditable(false);
//		cbToMonth.setSelectedItem(tc.getMonth());
//		cbToMonth.setBounds(207, 70, 53, 20);
//		contentPane.add(cbToMonth);
//		
//		cbToYear = new JComboBox(listOfYears());
//		cbToYear.setToolTipText("\u00C5r");
//		cbToYear.setEditable(false);
//		cbToYear.setSelectedItem(tc.getYear());
//		cbToYear.setBounds(270, 70, 53, 20);
//		contentPane.add(cbToYear);
//
//		cbToHour = new JComboBox(listOfHours());
//		cbToHour.setBounds(333, 70, 53, 20);
//		contentPane.add(cbToHour);
//		
//		cbToQuarter = new JComboBox(listOfQuarters());
//		cbToQuarter.setBounds(396, 70, 53, 20);
//		contentPane.add(cbToQuarter);
		
		cbCompany = new JComboBox(listOfCNames());
		cbCompany.setEditable(false);
		cbCompany.setBounds(144, 51, 305, 20);
		contentPane.add(cbCompany);
		
		txtTitle = new JTextField();
		txtTitle.addKeyListener(new KeyListener() 
		{
			
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
//				setCity();
//				checkLength();
			}
			
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		txtTitle.setBounds(146, 92, 80, 20);
		getContentPane().add(txtTitle);
		txtTitle.setColumns(10);	
//		
//		txtCity = new JTextField();
//		txtCity.setEditable(false);
//		txtCity.setBounds(234, 134, 215, 20);
//		contentPane.add(txtCity);
//		txtCity.setColumns(10);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(144, 142, 305, 106);
		contentPane.add(scrollPane);		
		txtText = new JTextArea();
		scrollPane.setViewportView(txtText);
		txtText.setLineWrap(true);			
		
		/**
		 * Setup buttons
		 */		
		JButton btnSave = new JButton("Gem");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (createAppointment()) 
					closeWindow();
			}
		});
		btnSave.setBounds(261, 261, 89, 23);
		contentPane.add(btnSave);
		
		JButton btnCancel = new JButton("Annuller");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWindow();
			}
		});
		btnCancel.setBounds(360, 261, 89, 23);
		contentPane.add(btnCancel);	
		
		lblLblerror = new JLabel("");
		lblLblerror.setForeground(Color.RED);
		lblLblerror.setBounds(10, 286, 214, 14);
		contentPane.add(lblLblerror);
	}
	
//	private void setCity()
//	{
//		try {
//			if( zcc.findZipCode(Integer.parseInt(txtZipCode.getText())) != null)
//			{
//				int zipcode = Integer.parseInt(txtZipCode.getText());
//				ZipCode zc = zcc.findZipCode(zipcode);
//				
//				txtCity.setText(zc.getCity());
//			}	
//		} catch (NumberFormatException e) {
//			setError("Kun tal i postnr. feltet!");
//		}
//	}
	
//	private void checkLength()
//	{
//		String string = txtZipCode.getText();
//		if (string.length()>4) {
//			txtZipCode.setText(string.substring(0, 4));
//		}
//	}

	private void closeWindow() 
	{
		dispose();
	}	
	
	private Object[] listOfKPNames()
	{
		KeyPersonCtrl kpc = new KeyPersonCtrl();
		ArrayList<KeyPerson> kpa = new ArrayList<KeyPerson>();
		
		kpa = kpc.listOfAllKeyPerson();
		return kpa.toArray();
	}
	
	private Object[] listOfCNames()
	{
		CompanyCtrl cc = new CompanyCtrl();
		ArrayList<Company> ca = new ArrayList<Company>();
		
		ca = cc.listOfAllCompany();
		return ca.toArray();
	}
	
//	private Object[] listOfDays()
//	{		
//		while(dayCount <= 31)
//		{
//			days.add(dayCount);
//			dayCount++;
//		}
//		
//		return days.toArray();
//	}
//	
//	private Object[] listOfMonths()
//	{
//		while(monthCount <= 12)
//		{
//			months.add(monthCount);
//			monthCount++;
//		}
//		
//		return months.toArray();
//	}
//	
//	private Object[] listOfYears()
//	{
//		while(yearCountMin <= yearCountMax)
//		{
//			years.add(yearCountMin);
//			yearCountMin++;
//		}
//		
//		return years.toArray();
//	}
//	
//	private Object[] listOfHours()
//	{		
//		while(hourCount <= 23)
//		{
//			hours.add(hourCount);
//			hourCount++;
//		}
//		
//		return hours.toArray();
//	}
//	
//	private Object[] listOfQuarters()
//	{
//		while(quarterCount <= 45)
//		{
//			quarters.add(quarterCount);
//			quarterCount = quarterCount + 15;
//		}
//		
//		return quarters.toArray();
//	}
	
	private boolean createAppointment()
	{
		boolean success = false;
		NoteCtrl nc = new NoteCtrl();
		
		String text;
//		long fromDate;
//		long toDate; 
//		String address;
//		int zipcode;
		long addedDate;
		KeyPerson kp;
		Company c;
//		ZipCode zc;
		String title;
		
		try
		{
			text = txtText.getText();		
			title = txtTitle.getText();
//			zipcode = Integer.parseInt(txtZipCode.getText());
//			zc = zcc.findZipCode(zipcode);
			addedDate = tc.getTimestampJava();
			kp = (KeyPerson) cbKeyPerson.getSelectedItem();
			c = (Company) cbCompany.getSelectedItem();
//			fromDate = new TimestampCtrl( (Integer) cbFromYear.getSelectedItem(), (Integer) cbFromMonth.getSelectedItem(), (Integer) cbFromDay.getSelectedItem(), (Integer) cbFromHour.getSelectedItem(), (Integer) cbFromQuarter.getSelectedItem()).getTimestampJava();
//			toDate = new TimestampCtrl( (Integer) cbToYear.getSelectedItem(), (Integer) cbToMonth.getSelectedItem(), (Integer) cbToDay.getSelectedItem(), (Integer) cbToHour.getSelectedItem(), (Integer) cbToQuarter.getSelectedItem()).getTimestampJava();
			
			if (!title.isEmpty() && !text.isEmpty() && kp!=null && c!=null) {
				success = true;
			} else {
				setError("Inkorrekte data!");
			}
			
			if (success) {
				nc.createNote(title, text, addedDate, kp, c);
			}
		}
		catch(NumberFormatException e)
		{
			setError("Error!");
		}	
		
		return success;
	}

	private void setError(String err) {
		lblLblerror.setText(err);
	}
}

